
!! Specifications of data types employed within the solver.
!> Precision of all real variables; length of strings.
!!
module ModDataTypes

    implicit none
  
    integer, parameter :: chrlen = 256         !< length of strings
    integer, parameter :: rtype  = kind(1.D0)  !< reals with double precision
  
  end module ModDataTypes
  

!! Variables controlling execution of the program.
!> Variables related to program control.
!!
module ModControl

    use ModDataTypes
    implicit none
  
    character(1) :: lrest !< use of previous solution for restart ("Y"=yes, "N"=no)
  
    integer :: maxiter, & !< max. number of iterations
               outstep, & !< number of iterations between solution dumps
               iter       !< actual iteration number
  
    real(rtype) :: convtol !< convergence criterion (2-norm of density change for
                           !! which the iteration process is stopped)
  
  end module ModControl

    
!! Names of files and numbers of I/O channels.
!> Variables and parameters related to file I/O.
!!
module ModFiles

    use ModDataTypes
    implicit none
  
    character(chrlen) :: fnGrid, & !< grid data
                         fnGtop, & !< grid topology and boundary conditions
                         fnFlow, & !< flow field (+ 5 digit iteration number + .v2d)
                         fnSurf, & !< quantities along wall surface(s) (+ 5 digit iteration number + .v2d)
                         fnConv, & !< convergence history (+ .v2d)
                         fnRsti, & !< restart solution - input
                         fnRsto    !< restart solution - output
  
    integer, parameter :: ifInp  = 10, & !< user input file (name stored in main.f90)
                          ifGrid = 20, &
                          ifGtop = 30, &
                          ifFlow = 40, &
                          ifSurf = 50, &
                          ifConv = 60, &
                          ifRsti = 70, &
                          ifRsto = 80
  
  end module ModFiles
  

!! Grid dimensions and geometry; boundary segments.
!> Variables related to grid geometry and topology.
!!
module ModGeometry

    use ModDataTypes
    implicit none
    ! 以单元编号为主体
    integer :: nci, &  !< number of grid cells in i-direction
               ncj, &  !< number of grid cells in j-direction
               imax, & !< max. dimensions in i-direction (0 <= i <= imax); imax = nci + 3
               jmax, & !< max. dimensions in j-direction (0 <= j <= jmax); jmax = ncj + 3
               il,  &  !< index of the 1st dummy cell in i-direction (right side); il = imax - 1
               jl,  &  !< index of the 1st dummy cell in j-direction (top side); jl = jmax - 1
               i2,  &  !< index of the last physical cell in i-direction; i2 = imax - 2 (physical cells start at index 2)
               j2      !< index of the last physical cell in j-direction; j2 = jmax - 2 (physical cells start at index 2)
  
    integer :: nsegs  !< total number of boundary segments
    integer, allocatable :: lbsegs(:,:) !< description of boundary segments\n
      !! @details
      !! Meaning of the entries:
      !! @li (*,1) = boundary type (bctype):\n
      !!             100-199 = inflow\n
      !!             200-299 = outflow\n
      !!             300-399 = viscous wall\n
      !!             400-499 = inviscid wall\n
      !!             500-599 = symmetry line\n
      !!             600-699 = far-field\n
      !!             700-799 = coordinate cut or periodic boundary\n
      !!             800-899 = mass injection
      !! @li (*,2) = side of the computational domain (1: j=2, 2: i=i2, 3: j=j2,
      !!             4: i=2; see Fig. 12.2) 四个面中的哪一个,(索引不变作为标示)
      !! @li (*,3) = start index of the segment (====given as cell index===, NOT node
      !!             index)
      !! @li (*,4) = end index of the segment
      !! @li (*,5) = side of the computational domain where the source (partner)
      !!             segment is located (if 700 <= bctype <= 799)
      !! @li (*,6) = start index of the source segment
      !! @li (*,7) = end index of the source segment
  
    real(rtype) :: xref, & !< x-coordinate of the reference point
                   yref, & !< y-coordinate of the reference point
                   cref    !< reference length or airfoil chord
  
    real(rtype), allocatable :: x(:,:),    & !< x-coordinates of grid points
                                y(:,:),    & !< y-coordinates of grid points
                                si(:,:,:), & !< x,y-components of the face vector (n*dS) in i-direction (see Fig. 12.3)
                                sj(:,:,:), & !< x,y-components of the face vector in j-direction
                                vol(:,:)     !< cell volume (= control volume)
  
  end module ModGeometry

!! Physical variables, settings and boundary conditions.
!! Note that all quantities are expected in SI-units.  
!> Variables related to physics and boundary conditions.
!!
module ModPhysics

    use ModDataTypes
    implicit none
  
    character(1) :: kequs, & !< equations solved ("E"=Euler, "N"=Navier-Stokes)
                    kflow    !< type of flow ("E"=external, "I"=internal)
  
  ! reference values
  
    real(rtype) :: gamma,  & !< ratio of specific heat coefficients
                   cpgas,  & !< specific heat coefficient at constant pressure
                   prlam,  & !< laminar Prandtl number
                   renum,  & !< Reynolds number
                   refvel, & !< reference velocity (internal flow only; for external flow computed from the far-field boundary)
                   refrho, & !< reference density (internal flow only; for external flow computed from the far-field boundary)
                   refvisc   !< reference dynamic viscosity coefficient (computed from renum, refvel, cref and refrho)
  
  ! boundary conditions - external flow
  
    real(rtype) :: machinf, & !< Mach-number at infinity
                   alpha,   & !< angle of attack
                   pinf,    & !< static pressure at infinity
                   tinf,    & !< static temperature at infinity
                   rhoinf,  & !< density at infinity
                   uinf,    & !< u-component of velocity vector at infinity
                   vinf,    & !< v-component of velocity vector at infinity
                   qinf       !< total velocity (= SQRT(uinf**2+vinf**2))
  
  ! boundary conditions - internal flow
  
    real(rtype) :: ptinl,   & !< total pressure at inlet
                   ttinl,   & !< total temperature at inlet
                   betainl, & !< low angle at inlet (with x-axis, positive in the clock-wise direction)
                   betaout, & !< approximate outlet angle (utilized for the initial guess only)
                   p12rat,  & !< ratio of inlet to outlet static pressure (initial guess only)
                   pout       !< static pressure at outlet
  
  ! boundary conditions - injection
  
    real(rtype) :: minject, & !< mass flow rate [kg/m^2*s]
                   tinject    !< injection temperature [K]
  
  ! flow variables
  
    integer :: nconv, & !< number of conservative variables (cv)
               ndepv    !< number of dependent variables (dv)
  
    real(rtype), allocatable :: cv(:,:,:) !< conservative variables\n
                !! @details
                !! cv(1,i,j) = density\n
                !! cv(2,i,j) = density * u\n
                !! cv(3,i,j) = density * v\n
                !! cv(4,i,j) = density * E
  
    real(rtype), allocatable :: dv(:,:,:) !< dependent variables\n
                !! @details
                !! dv(1,i,j) = static pressure\n
                !! dv(2,i,j) = static temperature\n
                !! dv(3,i,j) = speed of sound\n
                  ! 对于常用的空气  gamma 与 Cp, Cv  均为定值
                !! dv(4,i,j) = ratio of specific heats\n
                !! dv(5,i,j) = specific heat coefficient at constant pressure\n
                  ! 层流粘性系数使用沙特兰公式计算, 层流热导热系数 kapa = mu*Cp/Pr
                !! dv(6,i,j) = laminar viscosity coefficient (if viscous flow)\n
                !! dv(7,i,j) = laminar heat conductivity coefficient (if viscous flow)
  
  end module ModPhysics

    
!> Variables related to plot output.
!!
module ModPlotQuant

    use ModDataTypes
    implicit none
  
    integer, parameter :: mxquant =13, & !< total number of plot variables
                          mxqfield=11    !< no. of plot variables in the field (cf and Cp only at the boundaries)
  
    character(chrlen) :: title           !< title of the simulation case
    character(chrlen) :: cquant(mxquant) !< names of plot variables
    character(1)      :: lquant(mxquant) !< on/off switches of the plot variables
  
    real(rtype) :: drho,  & !< change of the density residual (convergence criterion)
                   drho1, & !< initial change of the density residual (used for normalization)
                   cl,    & !< lift coefficient (pressure forces only; external flow)
                   cd,    & !< drag coefficient (pressure forces only; external flow)
                   cm,    & !< pitching moment coefficient wrp. to the reference point
                   mflow, & !< average mass flow rate (internal flow)
                   mfratio  !< ratio of mass flow at outlet to mass flow at inlet
  
  end module ModPlotQuant


!> Variables related to the numerics.
!!
module ModNumerics

  use ModDataTypes
  implicit none

  character(1) :: ktimst,  & !< switch between local (="L") and global (="G") time-stepping
                  lvort,   & !< far-field vortex correction ("Y"=yes, "N"=no)
                  kdissip, & !< central scheme (="C") or Roe's upwind scheme (="R")
                  kprecond   !< low Mach-number preconditioning ("Y"=yes, "N"=no)

  integer :: iorder,    & !< order of Roe's upwind scheme (1; 2: kappa=1/3 MUSCL scheme)
             nrk,       & !< number of stages (Runge-Kutta scheme); max. = 5
             iextrapol, & !< pressure extrapolation to solid walls (2- or 3-point formula)
             ldiss(5)     !< dissipation evaluation per stage (0=no, 1=yes)

  real(rtype) :: cfl,      & !< CFL-number
                 epsirs,   & !< coefficient of implicit residual smoothing
                 vis2,     & !< 2nd-order dissipation coefficient (central scheme)
                 vis4,     & !< 4th-order dissipation coefficient (central scheme)
                 limfac,   & !< limiter coefficient (Roe's upwind scheme)
                 epsentr,  & !< entropy correction coefficient (Roe's upwind scheme)
                 precoeff, & !< preconditioning parameter K (low Mach numbers)
                 ark(5),   & !< stage coefficients
                 betrk(5)    !< dissipation-blending coefficients
                ! 额外的保证
  real(rtype) :: maxwchg !< max. relative change of density and density*E at slip walls\n
                         !! @details
                         !! When the relative change exceeds this value, extrapolation
                         !! to the dummy cells at walls is reduced to 0th-order.
  real(rtype) :: maxichg !< max. relative change of density and density*E at injection boundaries\n
                         !! @details
                         !! When the relative change exceeds this value, extrapolation
                         !! to the dummy cells at injection boundaries is reduced to
                         !! 0th-order.
          
  real(rtype) :: volref    !< reference volume\n
                           !! @details
                           !! Parameter is required for the computation of limiter
                           !! functions (higher-order Roe scheme).
  real(rtype) :: limref(4) !< reference values of density, u, v and pressure\n
                           !! @details
                           !! Parameter is required for the computation of limiter
                           !! functions (higher-order Roe scheme).

  real(rtype), allocatable :: cvold(:,:,:), & !< conservative variables from previous time step
                              diss(:,:,:),  & !< artificial dissipation
                              rhs(:,:,:),   & !< residual (right-hand side)
                              sri(:,:),     & !< spectral radii in i-direction
                              srj(:,:),     & !< spectral radii in j-direction
                              epsij(:,:,:), & !< coefficients of implicit residual smoothing (x-, y-direction)
                              tstep(:,:)      !< time steps (without the CFL-number)

  real(rtype), allocatable :: dui(:,:,:) !< 1st differences of primitive variables\n
                              !! @details
                              !! Values of density, u, v, and pressure in the
                              !! i-direction at the cell face I-1/2 (higher-order
                              !! Roe scheme).
  real(rtype), allocatable :: duj(:,:,:) !< 1st differences of primitive variables\n
                              !! @details
                              !! Values of density, u, v, and pressure in the
                              !! j-direction at the cell face J-1/2 (higher-order
                              !! Roe scheme).

  real(rtype), allocatable :: gradfi(:,:,:) !< gradients of velocity components and temperature\n
                              !! @details
                              !! Gradients with respect to the x- and y-coordinates at
                              !! the cell faces I-1/2 (required for viscous flow).
  real(rtype), allocatable :: gradfj(:,:,:) !< gradients of velocity components and temperature\n
                              !! @details
                              !! Gradients with respect to the x- and y-coordinates at
                              !! the cell faces J-1/2 (required for viscous flow).

  real(rtype) :: pi, & !< 3.14...
                 rad   !< 180./pi

end module ModNumerics

